package org.oak.configuration;

import org.oak.interceptor.LoggingInterceptor;
import org.oak.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;

import javax.annotation.Resource;

/**
 * org.oak.configuration.WebConfig
 *
 * @author ZRS
 * @version V1.0
 * @since 2025/10/30 17:25
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    LoggingInterceptor loggingInterceptor;


    @Resource
    TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loggingInterceptor)
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns("/api/login", "/api/register", "/css/**", "/js/**", "/api/register", "/public/**")
                .order(1);

        // 注册Token拦截器，拦截所有请求
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                // 排除不需要拦截的路径，如登录接口
                .excludePathPatterns("/api/login", "/api/register", "/css/**", "/js/**", "/public/**")
                .order(2)
        ;
    }


    // 配置BeanNameUrlHandlerMapping
    @Bean
    public BeanNameUrlHandlerMapping handlerMapping() {
        return new BeanNameUrlHandlerMapping();
    }

    // 配置SimpleControllerHandlerAdapter
    @Bean
    public SimpleControllerHandlerAdapter handlerAdapter() {
        return new SimpleControllerHandlerAdapter();
    }
}
